package com.binance.notification.web.push;

import com.binance.master.utils.StringUtils;
import com.google.common.collect.Lists;

import java.util.Iterator;
import java.util.List;

/**
 * @author lufei
 * @date 2018/11/6
 */
public class Callback implements RequestCallback  {

    private String[] resultArr;

    public Callback(String[] resultArr) {
        this.resultArr = resultArr;
    }

    @Override
    public void completed(List<PushResult> pushResultList, Object... obj) {
        List<String> errors = Lists.newArrayList();
        for (PushResult pushResult : pushResultList) {
            if (pushResult.isResultOK()) {
                for (Iterator<PushResult> iter = pushResult.errIterator(); iter.hasNext();) {
                    PushResult pr = iter.next();
                    if (pr.error != null) {
                        errors.add(pr.error.getMessage());
                    }
                }
            } else {
                if (pushResult.getEx() != null) {
                    errors.add(pushResult.getEx().getMessage());
                } else {
                    errors.add("未知异常！");
                }
            }
        }
        this.resultArr[0] = StringUtils.join(errors, "|");
    }

}
